﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TeamFoundation.ContinuousMerge.BranchGraph
{
    /// <summary>
    /// Represents a single branch node.
    /// </summary>
    public class BranchNode : IEquatable<BranchNode>
    {
        /// <summary>
        /// Gets the server item.
        /// </summary>
        public string ServerItem
        {
            get;
            internal set;
        }

        #region IEquatable<BranchNode> Members

        bool IEquatable<BranchNode>.Equals(BranchNode other)
        {
            if (object.ReferenceEquals(other, null))
                return false;
            return other.ServerItem == this.ServerItem;
        }

        /// <summary>
        /// Gets a hash code for this <see cref="BranchNode"/>.
        /// </summary>
        /// <returns>The hashcode returned by <see cref="String.GetHashCode"/> against <see cref="ServerItem"/>.</returns>
        public override int GetHashCode()
        {
            if (ServerItem == null)
                return 0;
            else
                return ServerItem.GetHashCode();
        }

        /// <summary>
        /// Determines whether this object is seen as equal to another.
        /// </summary>
        /// <param name="obj">The object to compare this one to.</param>
        /// <returns>A value indicating whether this object is seen as equal to <see cref="obj"/>.</returns>
        public override bool Equals(object obj)
        {
            if (object.ReferenceEquals(obj, null))
                return false;
            if (obj is string)
            {
                return ServerItem == obj;
            }
            var bobj = obj as BranchNode;
            if (object.ReferenceEquals(bobj, null))
                return false;
            return ((IEquatable<BranchNode>)this).Equals(bobj);
        }
        #endregion

        /// <summary>
        /// Gets the string representation of the <see cref="BranchNode"/>.
        /// </summary>
        /// <returns>The value contained withing <see cref="ServerItem"/>.</returns>
        public override string ToString()
        {
            return ServerItem;
        }
    }
}
